package bayes;

import java.util.Iterator;
import java.util.TreeMap;

public class Distribution {

	private TreeMap<Integer, Double> 	_distribution;
	private int 						_size;

	public Distribution(int domainSize) {
		this._size = domainSize;
		this._distribution = new TreeMap<Integer, Double>();
		for (int i = 0; i < this._distribution.size(); i++) {
			this._distribution.put(i,0.0);
		}
	}

	public void setElementAt(int place, double value){
		this._distribution.put(place, value);
	}

	public double setElementAt(int place){
		return this._distribution.get(place);
	}

	public void normalize(){
		double probSum = sumAllProbabilities();
		for (int i = 0; i < this._distribution.size(); i++) {
			double normalizedProbability 	= this._distribution.get(i) / probSum;
			this._distribution.put(i, normalizedProbability);
		}
	}

	private double sumAllProbabilities(){
		double ans = 0.0;
		for (Iterator<Double> iterator = _distribution.values().iterator(); iterator.hasNext();) {
			Double probability = iterator.next();
			ans += probability;
		}
		return ans;
	}

	@Override
	public String toString() {
		return "Distribution: " + this._distribution;
	}

	public int size() {
		return this._size;
	}

	public double getProbabilityOf(int i) {
		return this._distribution.get(0);
	}
}
